package com.alex.algorithm.easy;

import java.util.HashMap;
import java.util.Map;

public class ContainDuplicate2 {

    public static void main(String[] args) {
        System.out.println(containDuplicate(new int[]{1,2,3,1}, 3));
        System.out.println(containDuplicate(new int[]{1,0,1,1}, 1));
        System.out.println(containDuplicate(new int[]{1,2,3,1,2,3}, 2));
    }

    public static boolean containDuplicate(int [] array, int k) {
        Map<Integer,Integer> valIndexMap = new HashMap();
        for (int i = 0; i < array.length; ++i) {
            if (valIndexMap.get(array[i]) != null && i - valIndexMap.get(array[i]) <= k) {
                return true;
            }
            valIndexMap.put(array[i],i);
        }
        return false;
    }
}
